import 'package:flutter/material.dart';
import 'package:flutter_logger_server/flutter_logger_server.dart';
import 'package:dio/dio.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final _flutterLoggerServerPlugin = FlutterLoggerServer.instance;

  @override
  void initState() {
    super.initState();
    _flutterLoggerServerPlugin.isDebug = true;
    _flutterLoggerServerPlugin.startServer();
  }

  @override
  void dispose() {
    super.dispose();
    _flutterLoggerServerPlugin.stopServer();
  }

  String responseData = """
  {
    "code": 0,
    "msg": "ok",
    "data": {
        "list": [
            {
                "ar_id": 851888,
                "invoice_no": "103000002588-2",
                "pro": 103000002588,
                "invoice_date": "2024-10-12",
                "invoice_due_date": "2024-10-12",
                "status_text": "Paid",
                "balance": "0",
                "invoice_serial": 2,
                "order_id": 6952825,
                "paid_amount": "3.31",
                "total_amount": 3.31
            },
            {
                "ar_id": 849263,
                "invoice_no": "103000002588-1",
                "pro": 103000002588,
                "invoice_date": "2024-07-25",
                "invoice_due_date": "2024-07-25",
                "status_text": "Paid",
                "balance": "0",
                "invoice_serial": 1,
                "order_id": 6952825,
                "paid_amount": "66.12",
                "total_amount": 66.12
            },
            {
                "ar_id": 848073,
                "invoice_no": "121000000045-2",
                "pro": 121000000045,
                "invoice_date": "2024-06-24",
                "invoice_due_date": "2024-06-24",
                "status_text": "Paid",
                "balance": "0",
                "invoice_serial": 2,
                "order_id": 6902201,
                "paid_amount": "0.97",
                "total_amount": 0.97
            },
            {
                "ar_id": 848072,
                "invoice_no": "121000000045-1",
                "pro": 121000000045,
                "invoice_date": "2024-06-24",
                "invoice_due_date": "2024-06-24",
                "status_text": "Paid",
                "balance": "0",
                "invoice_serial": 1,
                "order_id": 6902201,
                "paid_amount": "19.5",
                "total_amount": 19.5
            },
            {
                "ar_id": 848071,
                "invoice_no": "121000000044-2",
                "pro": 121000000044,
                "invoice_date": "2024-06-24",
                "invoice_due_date": "2024-06-24",
                "status_text": "Paid",
                "balance": "0",
                "invoice_serial": 2,
                "order_id": 6902197,
                "paid_amount": "0.97",
                "total_amount": 0.97
            },
            {
                "ar_id": 848070,
                "invoice_no": "121000000044-1",
                "pro": 121000000044,
                "invoice_date": "2024-06-24",
                "invoice_due_date": "2024-06-24",
                "status_text": "Paid",
                "balance": "0",
                "invoice_serial": 1,
                "order_id": 6902197,
                "paid_amount": "19.5",
                "total_amount": 19.5
            },
            {
                "ar_id": 848059,
                "invoice_no": "100000000284-2",
                "pro": 100000000284,
                "invoice_date": "2024-06-24",
                "invoice_due_date": "2024-06-24",
                "status_text": "Paid",
                "balance": "0",
                "invoice_serial": 2,
                "order_id": 6902077,
                "paid_amount": "0.97",
                "total_amount": 0.97
            },
            {
                "ar_id": 848058,
                "invoice_no": "100000000284-1",
                "pro": 100000000284,
                "invoice_date": "2024-06-24",
                "invoice_due_date": "2024-06-24",
                "status_text": "Paid",
                "balance": "0",
                "invoice_serial": 1,
                "order_id": 6902077,
                "paid_amount": "19.5",
                "total_amount": 19.5
            },
            {
                "ar_id": 848056,
                "invoice_no": "100000000283-2",
                "pro": 100000000283,
                "invoice_date": "2024-06-24",
                "invoice_due_date": "2024-06-24",
                "status_text": "Paid",
                "balance": "0",
                "invoice_serial": 2,
                "order_id": 6902019,
                "paid_amount": "0.97",
                "total_amount": 0.97
            },
            {
                "ar_id": 848055,
                "invoice_no": "100000000283-1",
                "pro": 100000000283,
                "invoice_date": "2024-06-24",
                "invoice_due_date": "2024-06-24",
                "status_text": "Paid",
                "balance": "0",
                "invoice_serial": 1,
                "order_id": 6902019,
                "paid_amount": "19.5",
                "total_amount": 19.5
            }
        ],
        "outstanding_balance": 0,
        "past_due_amount": 7086.15,
        "invoice_statistics": {
            "total": 144,
            "month_paid": 2,
            "total_past_due": 35,
            "total_outstanding": 0,
            "paid_applied": 69.43
        }
    }
}
  """;

  final Dio _dio = Dio();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Column(
          children: [
            Center(
              child: InkWell(
                  onTap: () async {
                    try {

                      String id = DateTime.now().toString();
                      final response = await _dio.get('http://www.baidu.com');

                      _flutterLoggerServerPlugin.messageLoggerPlugin.sendMessageLog(id: "1",createTime: DateTime.now(), message: "123");
                      _flutterLoggerServerPlugin.httpLoggerPlugin.sendHttpReq(
                          id: id,
                          createTime: DateTime.now(),
                          url: response.requestOptions.path,
                          method: "get",
                          headers: {"Content-Type2": 123, "Content-Type": "application/json2"},
                          body: '{"data":"123"}');
                      _flutterLoggerServerPlugin.httpLoggerPlugin
                          .sendHttpRes(id: id, createTime: DateTime.now().add(Duration(seconds: 11)), statusCode: 200, headers: {"Content-Type2": 123, "Content-Type": "application/json2"}, body: responseData);
                      print("发送成功");
                    } catch (e) {
                      print('Error sending request: $e');
                    }
                  },
                  child: Text('发送请求')),
            ),
          ],
        ),
      ),
    );
  }
}
